var express = require('express');
var router = express.Router();
var mechanismDao = require('../dao/mechanism');
var uploadPicHelper = require('../util/uploadPicHelper');
var AdminToken = require('../db/user_tokens');
var mechanismRoutes = module.exports;

/**
 * 获取机构信息列表  (管理员)
 * @param  token
 * @param  id
 * @param  mechanism_name
 *         url/mechanism/list
 */
mechanismRoutes.list = function(req, res) {
    var par = paramAll(req);
    par.size = !par.size ? 10 : parseInt(par.size);
    par.start = !par.page ? 0 : par.start = (parseInt(par.page) - 1) * parseInt(par.size);
    AdminToken.checkAdminLogin(par.token, null, function(err, errCode, adminInfo) {
        if(err) return  res.json(new ERR(err, errCode));
        mechanismDao.list(par, function(err, result) {
            if(err) return res.json(new ERR(err, 400));
            res.json(new PKG(result));
        })
    });
}
router.post('/list', mechanismRoutes.list);

/**
 * 审核机构  (管理员)
 * @param    token
 * @param    ids
 * @param    status    0    1
 *           url/mechanism/auth
 */
mechanismRoutes.auth = function(req, res) {
    var par = paramAll(req);
    if(!par.ids) return res.json(new ERR('ids不为空！', 400));
    par.ids = JSON.parse(par.ids);
    AdminToken.checkAdminLogin(par.token, null, function(err, errCode, adminInfo) {
        if(err) return  res.json(new ERR(err, errCode));
        mechanismDao.auth(par, function(err, result) {
            if(err) return res.json(new ERR(err, 400));
            res.json(new PKG(result));
        })
    });
}
router.post('/auth', mechanismRoutes.auth);

/**
 * 机构信息录入
 * @param  openid
 * @param  mechanism_name    头像
 * @param  mechanism_avatar
 * @param  telephone
 * @param  province
 * @param  city
 * @param  area
 * @param  address
 * @param  des
 * @param  ext_info       是一个数组   放图片集合
 * @param  id
 *
 * @param  crowds
 * @param  services
 *         url/mechanism/save
 */
mechanismRoutes.save = function(req, res) {
    var par = paramAll(req);
    var mechanismInfo = {};
    mechanismInfo.ext_info = par.ext_info;
    if(par.des) mechanismInfo.des = par.des;
    if(par.id) mechanismInfo.id = par.id;
    ['mechanism_name', 'openid', 'mechanism_avatar', 'telephone', 'province', 'city', 'area', 'address'].forEach(function(k) {
        mechanismInfo[k] = par[k];
    })
    mechanismDao.save(mechanismInfo, function(err, result) {
        if(err) return res.json(new ERR(err, 400));
        res.json(new PKG(result));
    });
}
router.post('/save', mechanismRoutes.save);

/**
 * 机构信息录入
 * @param  img
 *         url/mechanism/uploadImg
 */
function checkImgParam(par, cb) {
    var picKeys = ['img'];
    if(uploadPicHelper.checkPicName(picKeys, par.picNames)) return cb('图片参数有误', 400);
    cb(null, 0, par);
}
mechanismRoutes.uploadImg = function(req, res) {
    uploadPicHelper.uploadPicsAndCheckPar(req, checkImgParam, 9, 'mechanism', false, function(err, errCode, par) {
        if(err) return res.json(new ERR(err, errCode));
        res.json(new PKG({img: par.img}));
    });
}
router.post('/uploadImg', mechanismRoutes.uploadImg);

/**
 * 获取专属二维码
 * @param scene   传  机构id
 * @param width
 *         url/mechanism/getQrCode
 */
mechanismRoutes.getQrCode = function(req, res) {
    var par = paramAll(req);
    if(!par.scene) return res.json(new ERR('scene不为空!', 400));
    if(!par.width) return res.json(new ERR('width不为空!', 400));
    if(!par.auto_color) par.auto_color = false;
    if(!par.line_color) par.line_color = {r:"0", g:"0", b:"0"};
    mechanismDao.getQrCode(par, function(err, result) {
        if(err) return res.json(new ERR(err, 400));
        res.json(new PKG(result));
    })
}
router.post('/getQrCode', mechanismRoutes.getQrCode);

/**
 * 机构详情
 * @param openid
 *         url/mechanism/getMechanismInfo
 */
mechanismRoutes.getMechanismInfo = function(req, res) {
    var par = paramAll(req);
    if(!par.openid) return res.json(new ERR('openid不为空!', 400));
    mechanismDao.getMechanismInfo(par, function(err, result) {
        if(err) return res.json(new ERR(err, 400));
        res.json(new PKG(result));
    })
}
router.post('/getMechanismInfo', mechanismRoutes.getMechanismInfo);

module.exports = router;